package com.bw.documents.local

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.common.mvi.BaseActivity
import com.bw.documents.databinding.ActivityLocalBinding
import com.bw.documents.local.adapter.LocalDocumentsAdapter
import com.bw.documents.local.entity.LocalDocumentsType
import kotlinx.coroutines.launch

@Route(path = "/documents/LocalActivity")
class LocalActivity : BaseActivity<ActivityLocalBinding,LocalViewModel>() {

    private val localAdapter : LocalDocumentsAdapter by lazy { LocalDocumentsAdapter() }

    override fun createVM(): LocalViewModel {
        return viewModels<LocalViewModel>().value
    }

    override fun initView() {
        v.localPageInclude.titleTv.text = "本地文件选择"
        v.localPageInclude.moreTv.text = "上传"
        v.localPageInclude.moreTv.setOnClickListener {
            val bund = Bundle()
            localAdapter.data.forEach {
                if (it.sel){
                    bund.putString("name",it.name)
                    bund.putString("path",it.path)
                }
            }
            val intent = Intent()
            intent.putExtras(bund)
            setResult(RESULT_OK,intent)
            finish()
        }
        v.localPageInclude.backImg.setOnClickListener { finish() }
        val manager = LinearLayoutManager(this)
        v.localRv.layoutManager=manager
        localAdapter.setOnItemCLickListener{adapter,position->
            when(localAdapter.data[position].type){
                LocalDocumentsType.DOCUMENTS->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/PDFActivity").with(bund).navigation()
                }
                LocalDocumentsType.IMG->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/PhotoActivity").with(bund).navigation()
                }
                LocalDocumentsType.VIDEO->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/VideoActivity").with(bund).navigation()
                }
                LocalDocumentsType.MUSIC->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/MusicActivity").with(bund).navigation()
                }
            }
        }
        localAdapter.setOnItemChildCLickListener{adapter,id,position->
            var flag = localAdapter.data[position].sel
            localAdapter.data.forEach{
                it.sel = false
            }
            localAdapter.data[position].sel = !flag
            localAdapter.notifyDataSetChanged()
        }
        v.localRv.adapter = localAdapter
    }

    override fun initData() {
        vm.scanFiles()
    }

    override fun bindState() {
        lifecycleScope.launch {
            vm.mutableContainer.uiStateFlow.collect{
                if (it.localState){
                    localAdapter.setNewInstance(it.datas)
                }
            }
        }
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityLocalBinding.inflate(LayoutInflater.from(this))
    }
}